package cn.tedu.fengyuback.personal.service.impl;

import cn.tedu.fengyuback.base.constent.Constant;
import cn.tedu.fengyuback.common.LikeFavorite;
import cn.tedu.fengyuback.personal.mapper.PersonalMapper;
import cn.tedu.fengyuback.personal.pojo.dto.ArticlesQuery;
import cn.tedu.fengyuback.personal.pojo.dto.FavoritesQuery;
import cn.tedu.fengyuback.personal.pojo.dto.LikesQuery;
import cn.tedu.fengyuback.personal.pojo.dto.UserQuery;
import cn.tedu.fengyuback.personal.pojo.entity.Articles;
import cn.tedu.fengyuback.personal.pojo.entity.Favorites;
import cn.tedu.fengyuback.personal.pojo.entity.Likes;
import cn.tedu.fengyuback.personal.pojo.entity.UserFollows;
import cn.tedu.fengyuback.personal.pojo.vo.*;
import cn.tedu.fengyuback.personal.service.PersonalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class PersonalServiceImpl implements PersonalService {
    /**
     * 注入个人主页持久层对象
     */
    @Autowired
    PersonalMapper personalMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * 查询用户关注列表
     * @param userQuery
     * @return
     */
    @Override
    public List<UserFollowsVO> selectUserFollows(UserQuery userQuery) {
        List<UserFollowsVO> list = personalMapper.selectUserFollows(userQuery.getId());
        //获取关注用户的昵称
        for (UserFollowsVO userFollowsVO : list){
            userQuery.setId(userFollowsVO.getFollowsUserId());
            UserVO userVO = personalMapper.select(userQuery);
            userFollowsVO.setNickname(userVO.getNickname());

        }
        return list;
    }
    @Override
    public List<UserVO> selectUserUnFollows(Long id) {
        log.debug("查询未关注用户的参数："+id);
        List<UserVO> list = personalMapper.selectUserUnFollows(id);
        log.debug("未关注用户的集合："+list);
        //获取未关注用户的昵称
        for (UserVO userVO : list){
            PersonalQuery personalQuery = new PersonalQuery();
            personalQuery.setId(userVO.getId());
            log.debug("查询未关注用户的参数："+personalQuery);
            UserVO userVO1 = personalMapper.select(personalQuery);
            log.debug("未关注用户的昵称："+userVO1.getNickname());
            userVO.setNickname(userVO1.getNickname());
        }
        log.debug("未关注用户的集合："+list);
        return list;


    }

    /**
     * 取消关注
     * @param followsUserId
     */
    @Override
    public void addFollows(Long id,Long data) {
        log.debug("关注用户的id："+id);
        log.debug("用户的id："+data);
        UserFollows userFollows = new UserFollows();
        userFollows.setUserId(data);
        userFollows.setFollowsUserId(id);
        personalMapper.insert(userFollows);
        log.debug("关注用户的参数："+userFollows);
    }
    //发表文章
    @Override
    public void releaseArticle(ArticlesPram articlesPram) {
        log.debug("发布文章的参数："+articlesPram);
        Articles articles = new Articles();
        BeanUtils.copyProperties(articlesPram,articles);
        articles.setQuestionId(1L);
        articles.setLikes("0");
        articles.setFavorites("0");
        articles.setViews("100");
        articles.setStatus("1");
        articles.setCreateTime(new Date());
        personalMapper.insertArticles(articles);
        log.debug("发布文章的参数："+articles);
    }


    //新增关注


    @Override
    public void deleteByFollowsUserId(Long followsUserId) {
        personalMapper.deleteByFollowsUserId(followsUserId);
    }

    /**
     * 查询用户发布文章
     * @param articlesQuery
     * @return
     */
    @Override
    public List<ArticlesVO> selectByIdArticle(ArticlesQuery articlesQuery) {
        List<ArticlesVO> list = personalMapper.selectByIdArticle(articlesQuery);
        //查询用户发布的文章，用户自己是否点赞、收藏
        for (ArticlesVO articlesVO : list){
            //查询是否点赞
            likesQuery(articlesVO);
            //查询是否收藏
            favoritesQuery(articlesVO);
        }
        return list;
    }

    /**
     * 查询用户是否收藏该文章
     * @param articlesVO
     */
    public void favoritesQuery(ArticlesVO articlesVO) {
        FavoritesQuery favoritesQuery = new FavoritesQuery();
        favoritesQuery.setArticleId(articlesVO.getId());
        favoritesQuery.setUserId(articlesVO.getUserId());
        FavoritesVO favoritesVO = personalMapper.selectFavorites(favoritesQuery);
        if (favoritesVO != null){//收藏了
            articlesVO.setFavoritesId(favoritesVO.getId());
            articlesVO.setFavoritesStatus(Constant.ARTICLE_LIKE_FAVORITES);
        }else {//没收藏
            articlesVO.setFavoritesStatus(Constant.NOT_ARTICLE_LIKE_FAVORITES);
        }
    }

    /**
     * 查询用户是否点赞该文章
     * @param articlesVO
     */
    public void likesQuery(ArticlesVO articlesVO) {
        LikesQuery likesQuery = new LikesQuery();
        likesQuery.setArticleId(articlesVO.getId());
        likesQuery.setUserId(articlesVO.getUserId());
        LikesVO likesVO = personalMapper.selectLikes(likesQuery);
        if (likesVO != null){//点赞了
            articlesVO.setLikesId(likesVO.getId());
            articlesVO.setLikesStatus(Constant.ARTICLE_LIKE_FAVORITES);
        }else {//没点赞
            articlesVO.setLikesStatus(Constant.NOT_ARTICLE_LIKE_FAVORITES);
        }
    }

    /**
     * 修改文章点赞状态
     * @param likesQuery
     */
    @Override
    public void changeLikesStatus(LikesQuery likesQuery) {
        LikesVO likesVO = personalMapper.selectLikes(likesQuery);
        Likes likes = new Likes();
        BeanUtils.copyProperties(likesQuery,likes);
        //判断文章的点赞状态
        if (likesVO == null){//文章未点赞，进行点赞操作
            likes.setLikeTime(new Date());
            personalMapper.insertLikes(likes);
            addLike(likesQuery);
        }else {//文章已点赞，取消点赞操作
            personalMapper.deleteLikes(likes);
            minusLike(likesQuery);
        }
    }

    /**
     * 减少文章点赞数
     * @param likesQuery
     */
    private void minusLike(LikesQuery likesQuery) {
        Articles articles = new Articles();
        Long num = likesQuery.getLikes() - 1;
        articles.setLikes(num.toString());
        articles.setId(likesQuery.getArticleId());
        personalMapper.updateArticles(articles);
    }

    /**
     * 增加文章表点赞数
     * @param likesQuery
     */
    private void addLike(LikesQuery likesQuery) {
        Articles articles = new Articles();
        Long num = likesQuery.getLikes() + 1;
        articles.setLikes(num.toString());
        articles.setId(likesQuery.getArticleId());
        personalMapper.updateArticles(articles);
    }

    /**
     * 修改文章收藏状态
     * @param favoritesQuery
     */
    @Override
    public void changeFavoritesStatus(FavoritesQuery favoritesQuery) {
        FavoritesVO favoritesVO = personalMapper.selectFavorites(favoritesQuery);
        Favorites favorites = new Favorites();
        BeanUtils.copyProperties(favoritesQuery,favorites);
        //判断文章的收藏状态
        if (favoritesVO == null){//为收藏
            favorites.setFavoritesTime(new Date());
            personalMapper.insertFavorites(favorites);
            addArticles(favoritesQuery);
        }else {//已收藏
            personalMapper.deleteFavorites(favorites);
            minusArticles(favoritesQuery);
        }
    }



    /**
     * 减少文章收藏数
     * @param favoritesQuery
     */
    private void minusArticles(FavoritesQuery favoritesQuery) {
        Articles articles = new Articles();
        Long num = favoritesQuery.getFavorites() - 1;
        articles.setFavorites(num.toString());
        articles.setId(favoritesQuery.getArticleId());
        personalMapper.updateArticles(articles);
    }

    /**
     * 增加文章表收藏数
     * @param favoritesQuery
     */
    private void addArticles(FavoritesQuery favoritesQuery) {
        Articles articles = new Articles();
        Long num = favoritesQuery.getFavorites() + 1;
        articles.setFavorites(num.toString());
        articles.setId(favoritesQuery.getArticleId());
        personalMapper.updateArticles(articles);
    }
}
